<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace yiike\user\controllers;

use Yii;
use yiike\user\models\Token;

/**
 * 
 * @author Sylar
 */
class UpdateController extends \api\components\UserController {
    
    /**
     * 更新个人资料
     * @return boolean
     */
    public function actionProfile() {
        $data = Yii::$app->request->post();
        $profile = \yiike\user\models\Profile::findOne($this->user->id);
        $profile->setAttributes($data);
        
        if (! $profile->save()) {
            $errors = array();
            foreach ($profile->errors as $k => $v) {
                $errors[$k] = $v[0];
            }
            return ['code' => 100034, 'errors' => $errors];
        }
        
        $pid = Yii::$app->request->post('pid');
        $openid = Yii::$app->request->post('openid');
        $model = \yiike\iot\models\DeviceUser::findOne(['user_id' => $this->user->id, 'product_id' => $pid, 'status' => 1]);
        if ($model) {
            $device  = $model->device;
            $wechat  = \yiike\wechat\models\Wechat::findOne($model->wid);
            $blue    = new \yiike\iot\components\blue\Info(['wechat' => $wechat, 'device' => $device, 'openid' => $openid]);
            $ret     = $blue->setProfile($profile);
        }
        return true;
    }
    
    /**
     * 更新所在城市
     * @return boolean
     */
    public function actionLocation() {
        $lat = Yii::$app->request->post('lat');
        $lng = Yii::$app->request->post('lng');
        $address = Yii::$app->geo->getAddress($lng, $lat);
        
        if (isset($address['result']['addressComponent']['city'])) {
            $profile = \yiike\user\models\Profile::findOne($this->user->id);
            $profile->city = $address['result']['addressComponent']['city'];
            
            if (! $profile->save()) {
                $errors = array();
                foreach ($profile->errors as $k => $v) {
                    $errors[$k] = $v[0];
                }
                return ['code' => 100034, 'errors' => $errors];
            }
        }
        return true;
    }
    
    /**
     * 重置密码
     * @return boolean
     */
    public function actionPassword() {
        $data = Yii::$app->request->post();
        $passwordForm = new \yiike\user\models\PasswordForm();
        $passwordForm->setAttributes($data);
        if (! $passwordForm->save()) {
            $errors = array();
            foreach ($passwordForm->errors as $k => $v) {
                $errors[$k] = $v[0];
            }
            return ['code' => 100033, 'errors' => $errors];
        }
        return true;
    }
    
    /**
     * 上传头像
     * @return boolean
     */
    public function actionAvatar() {
        $imageFile = \yii\web\UploadedFile::getInstanceByName('avatar');
        if ($imageFile) {
            $profile = $this->user->profile;
            $profile->setAvatarFile($imageFile);
            
            if ($profile->save()) {
                return ['code' => 0, 'avatar' => $profile->avatarUrl];                    
            }
        }
        return ['code' => 100035];
    }
    
    public function actionToken() {
        $token = Yii::$app->request->post('token') ? : Yii::$app->request->get('token');
        $tokenModel = Token::findOne(['code' => $token, 'type' => Token::TYPE_LOGIN]);
        if ($tokenModel && ($user = $tokenModel->user)) {
            $tokenModel->created_at = time();
            $tokenModel->save();
            return true;
        } else {
            return false;
        }
        
    }
}
